/**
 * Copyright (c) 2010, Casey Lai
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 
 * Neither the name of the Cheetah Group nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

package org.cheetahworkflow.designer.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.cheetahworkflow.designer.Activator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class XMLVerifier {

	private static final String SCHEMA_NAME = "/xsd/workflow.xsd";
	
	private static Schema schema = null;
	
	static {
		String LANGUAGE = XMLConstants.W3C_XML_SCHEMA_NS_URI;
		SchemaFactory factory = SchemaFactory.newInstance(LANGUAGE);
		try {
			schema = factory.newSchema(new StreamSource(Activator.class.getResourceAsStream(SCHEMA_NAME)));
		} catch (SAXException e) {
			MessageDialog.openError(null, "Error", "Cannot find workflow validator: " + SCHEMA_NAME + ". Validation action will not work.");
			e.printStackTrace();
		}
	}
	
	public static void verify(ErrorHandler handler, String xml) {
		if (schema != null) {
			Validator validator = schema.newValidator();
			validator.setErrorHandler(handler);
			try {
				Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))));
				validator.validate(xmlSource);
			} catch (UnsupportedEncodingException e) {
				// ignore it
			} catch (SAXException e) {
				// ignore it
			} catch (IOException e) {
				MessageDialog.openError(null, "Error", e.getMessage());
				e.printStackTrace();
			}
		} else {
			MessageDialog.openError(null, "Error", "Cannot find workflow validator: " + SCHEMA_NAME + ". Validation action will not work.");
		}
	}
}
